<?php

class phMegaupload {

	// /////////////////////////////////////////////////
	var $name = "phMegaupload";
	var $version = "r3";
	var $author = "Pyronhell";
	var $authorEmail = "webmaster@pyronhell.net";
	var $website = "http://www.pyronhell.net";
	// /////////////////////////////////////////////////
	
	// Url for retrieving data
	var $url = "http://www.megaupload.com";
	
	// Identify us as firefox 3.0.1
	var $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
	
	// Use premium?
	var $usePremium = false;
	
	// Premium cookie for retrieving links
	var $premiumUserHash = "";
	
	// Status code for errors or something else
	var $statusCode = array("Online", "Error retrieving link", "Offline", "", "", "", "", "", "", "Undefined error", 99 => 'Link do not exist');
	
	/*PREMIUM:
	<a onclick="loadingdownload();" class="downloadhtml" href="">Click here to download</a>
	
	CAPTCHA:
	<img src="/capgen.php?6cbc989cf4cb8e32"/>
	*/
	
	// For getting data for premium link
	function getDataForPremiumLink($html, $code)
	{
		/*preg_match("|var (.*) \= String\.fromCharCode\(Math.abs\((.*)\)\)\;|U", $html, $x);
		preg_match("|var (.*) \= \'(.*)\' \+ String\.fromCharCode\(Math.sqrt\((.*)\)\)\;|U", $html, $y);
		preg_match("|document\.getElementById\(\"download_html\"\)\.innerHTML \= \'<a href=\"(.*)\' \+ (.*) \+ (.*) \+ \'(.*)\" class=\"downloadhtml\"  onclick=\"loadingdownload\(\)\;\">Click here to download</font></a>\'\;|U", $html, $url_data);*/
     	//$return = @$url_data[1] . @$y[2] . @$this->charCode(@$y[3], "sqrt") . @$this->charCode(@$x[2], "abs") . @$url_data[4];
        
        $regex_finalurl = "|a href=\"http://(.*).megaupload.com/files/(.*)/(.*)\" onclick=\"\"|U";
        preg_match($regex_finalurl, $html, $return);             
        $return = "http://" . $return[1] . ".megaupload.com/files/" . $return[2] . "/" . $return[3];
        
		if (strlen($return)<(29+32))
		{
			$return = $this->url . "/?d=" . $code;
		}
		return $return;
	}
	
	
	function charCode($int, $type)
	{
		if ($type == "abs")
		{
			return chr(abs((int) $int));
		}
		elseif ($type == "sqrt")
		{
			return chr(sqrt((int) $int));
		}
	}
	
	function getDataFromCode($code)
	{
		// Url style: megaupload.com/?d=$code
		$url = $this->url . "/?d=" . $code . "&setlang=en";
		$html = $this->url_get($url);
		if (empty($html))
		{
			return array("statusCode" => 1);
		}
		else
		{
			if (strstr($html, '<div id="error">') !== FALSE)
			{
				return array("statusCode" => 2);
			}
			else
			{
				if ($this->usePremium)
				{
					if (strstr($html, ' id="downloadlink">') !== FALSE)
					{
						$data = array("statusCode" => 0);
                        $regex_name = "|Filename:</font> <font style=\"font-family:arial; color:#FF6700; font-size:15px; font-weight:bold;\">(.*)</font>|U";
                        $regex_description = "|File description:</font> <font style=\"font-family:arial; color:#000000; font-size:13px;\">(.*)</font>|U";
                        $regex_filesize = "|File size:</font> <font style=\"font-family:arial; color:#000000; font-size:13px;\">(.*)</font>|U";
                        $regex_finalurl = "|a href=\"http://(.*).megaupload.com/files/(.*)/(.*)\" onclick=\"\"|U";                
                        /*preg_match($regex_name, $html, $name);
						$data['short_filename'] = $name[1];*/
						preg_match($regex_finalurl, $html, $name);
                        $data['filename'] = $name[3];			
						preg_match($regex_filesize, $html, $size);
						$data['filesize'] = $size[1];
						preg_match($regex_description, $html, $desc);
						$data['description'] = $desc[1];
						$data['url'] = $this->url . "/?d=" . $code;
						$data['downloadUrl'] = $this->getDataForPremiumLink($html, $code);
						return $data;
					}
					else
					{
						return array("statusCode" => 99);
					}
				}
				else
				{
					if (strstr($html, '<a href="javascript:checkcaptcha();">') !== FALSE)
					{
						$data = array("statusCode" => 0);
                        $regex_name = "|Filename:</font> <font style=\"font-family:arial; color:#FF6700; font-size:15px; font-weight:bold;\">(.*)</font>|U";
                        $regex_description = "|File description:</font> <font style=\"font-family:arial; color:#000000; font-size:13px;\">(.*)</font>|U";
                        $regex_filesize = "|File size:</font> <font style=\"font-family:arial; color:#000000; font-size:13px;\">(.*)</font>|U";
						preg_match($regex_name, $html, $name);
						$data['short_filename'] = $name[1];
						$data['filename'] = $name[1];
                        preg_match($regex_filesize, $html, $size);
						$data['filesize'] = $size[1];
                        preg_match($regex_description, $html, $desc);
						$data['description'] = $desc[1];
						$data['url'] = $this->url . "/?d=" . $code;
						$data['downloadUrl'] = $this->url . "/?d=" . $code;
						return $data;
					}
					else
					{
						return array("statusCode" => 99);
					}
				}
			}
		}
	}
	
	function url_get($url)
	{
		if ($this->usePremium && !empty($this->premiumUserHash))
		{
			$cookie = 'l=en; user=' . $this->premiumUserHash;
		}
		else
		{
			$cookie = 'l=en';
		}
		if (function_exists('curl_init'))
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
			curl_setopt($ch, CURLOPT_FAILONERROR, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			curl_setopt($ch, CURLOPT_REFERER, $this->url);
			curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
			$get = curl_exec($ch);
			curl_close($ch);
			return $get;
		}
	}
	
	// Extract megaupload links from any text
	// If you don't have cURL on your server, you only can use this function
	function getLinks($s)
	{
		$links = array();
		preg_match_all("/http:\/\/(www\.)?(megaupload+\.[^.\s]+\.?[^.\s]*)+(\?d=[a-zA-Z0-9]*)/i", $s, $r);
		$i = 0;
		foreach ($r[0] as $val)
		{
			$links[$i]['url'] = $val;
			$links[$i]['code'] = str_replace("?d=", "", $r[3][$i]);
			$i++;
		}
		return $links;
	}
    
	function test()
	{
		echo "<b>Testing server for phMegaupload</b><br />";
		if (!function_exists('curl_init'))
		{
			echo "cURL: <b><font color='#F00'>NO</font></b><br />Class phMegaupload works only with cURL, so you only can use the link grabber.";
		}
		else
		{
			echo "cURL: <b><font color='#0F0'>YES</font></b><br />This class is fully compatible with your server.";
		}
	}
}

?>
